<template>
  <div class="swiper-wrap">
    <div class="major-event-detail-content scrollbar" @click.stop="showDetail(projectDetail)">
          <div style="height:112px">
            <p class="title" :title="projectDetail.projectName">{{projectDetail.projectName}}</p>
            <div class="info">
              <p class="desc" style="margin-right:10px" v-if="projectDetail.cityProjectTypeDesc">{{projectDetail.cityProjectTypeDesc}}</p>
              <p class="desc" style v-if="projectDetail.provinceProjectTypeDesc">{{projectDetail.provinceProjectTypeDesc}}</p> 
            </div>
            <div class="flex info-bottom">
              <!-- 实际开工时间 -->
              <p>开工时间 {{projectDetail.projectActStartTime || '-'}}</p>
              <div class="split-line"></div>
              <p>投资金额 {{projectDetail.projectTotalInvest || '-'}}</p>
              <span
                style="margin-left:2px"
              >{{projectDetail.projectTotalInvestUnit=='1'?'元':projectDetail.projectTotalInvestUnit=='2'?'万元':projectDetail.projectTotalInvestUnit=='3'?'亿':''}}</span>
            </div>
          </div>
          <div class="images" @click.stop>
            <div class="navigation" @click.stop="openNavigationList(projectDetail)">
              <img src="@/assets/project/navigation.png" alt />
              <div style="margin-left:2px">导航</div>
            </div>
            <div class="attention" @click.stop="focusChange(projectDetail)">
              <img :src="projectDetail.focusNot == '1'?focus:unFocus" />
              <div class="btn-name">{{projectDetail.focusNot=='1'?'已关注':'关注'}}</div>
            </div>
          </div>
          
        </div>
        <!-- 底部轮播删除 -->
    <!-- <van-swipe
      class="back-swipe"
      :height="179"
      :width="351"
      :show-indicators="false"
      :stop-propagation="false"
      @change="onChange"
      ref="refSwiper"
    >
      <van-swipe-item v-for="(projectDetail, index) in projectsList" :key="index" style="width: 351px">
        
      </van-swipe-item>
    </van-swipe> -->
    <div class="navigationList" v-if="navigationListIsShow" @click.stop>
      <p @click.stop="navigation('bd')">百度地图</p>
      <p @click.stop="navigation('gd')">高德地图</p>
      <p @click.stop="navigationListIsShow = false">取消</p>      
    </div>
    <!-- <van-loading v-if="myLocationStatus" vertical color="#1989fa">正在获取定位</van-loading> -->
    <van-overlay :show="navigationListIsShow" @click.stop="navigationListIsShow = false" />
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, reactive, toRefs, watch, onActivated, onMounted } from 'vue'
import { showFailToast } from 'vant'
import { useRouter } from 'vue-router'
import focus from '@/assets/project/attentioned.png'
import unFocus from '@/assets/project/attention.png'
import ProjectServe from '@/api/keyProject'

defineOptions({
  name: 'keyProjectDetail'
})

const props = defineProps({
  detail: {
    type: Object,
    default: {}
  },
  projectsList: {
    type: Array,
    default: []
  },
  activeTab: {
    type: Number,
    default: 0
  }
})
const isFocus = ref()
const router = useRouter()
const checkProjectDetail = ref({})
const currentProject = ref({})
const initial = ref(2)
const currentTab = computed(() => {
  return props.activeTab
})
const navigationListIsShow = ref(false)
const myLocation = reactive({
  lng: <any>null,
  lat: <any>null,
  city: null
})
const myLocationStatus = ref(false)
const refSwiper = ref<NullAble<HTMLElement>>(null)
const projectDetail = ref({});
watch(
  () => [props.detail,props.activeTab,props.projectsList],
  (newVal, oldVal) => {
    projectDetail.value = newVal[0];
    checkProjectDetail.value = newVal[0];
    isFocus.value = newVal[0].focusNot == '1' ? true : false;
    if(newVal[1] && newVal[1] !== oldVal[1]) {
      // 切换tab,轮播索引定位到第一个   
       swiperTo('0')
    } 
    if(newVal[0] && newVal[0] !== oldVal[0]) {
        swiperTo('');
    }   
  }
)
onMounted(() => {
  navigatorGeolocation()
})
const emit = defineEmits(['focus-click','item-click'])
computed(() => {})
// 点击取消关注
const focusChange = projectDetail => {
  // event.stopPropagation()
  emit('focus-click', projectDetail)
}
// 轮播索引定位
const swiperTo = (idx) => {
  let index = idx?idx:props.projectsList.findIndex(item => item.projectId == checkProjectDetail.value.projectId)
  if(!index || index < 0) {
    index = 0;
  }
  refSwiper.value.swipeTo(index)
}
const onChange = (index) => {
      let item = props.projectsList[index];
      emit('item-click', item)
    }
const navigatorGeolocation = () => {
  console.log('获取当前定位keyProjectDetail')
  // 获取当前定位
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
      //success
      function (position) {
        myLocation.lng = position.coords.longitude
        myLocation.lat = position.coords.latitude
        let e = {
          lnglat: { lng: position.coords.longitude, lat: position.coords.latitude }
        }
        myLocationStatus.value = true
        navigationListIsShow.value = false
      },
      //error
      function (error) {
        myLocationStatus.value = false
        navigationListIsShow.value = false
        switch (error.code) {
          case error.PERMISSION_DENIED:
            console.log('用户拒绝对获取地理位置的请求。')
            break
          case error.POSITION_UNAVAILABLE:
            console.log('位置信息是不可用的。')
            break
          case error.TIMEOUT:
            console.log('请求用户地理位置超时。')
            break
          case error.UNKNOWN_ERROR:
            console.log('未知错误。')
            break
        }
        // showFailToast('定位失败，请检查定位权限')
      },
      //options
      {
        timeout: 5000
      }
    )
  } else {
    console.log('不支持获取地理位置。')
  }
}

// 点击导航
const openNavigationList = projectDetail => {
  currentProject.value = projectDetail
  if (myLocationStatus.value) {
    navigationListIsShow.value = true
  } else {
    showFailToast('定位失败，请检查定位权限')
  }
}

const navigation = mapType => {
  let destination = currentProject.value
  if (!myLocation || !myLocation.lng || !myLocation.lat) {
    return
  }
  let Url = ''
  switch (mapType) {
    case 'gd': //高德地图
      Url = `https://uri.amap.com/navigation?from=${myLocation.lng},${myLocation.lat},起点&to=${destination.longitude},${destination.latitude},${destination.projectName}&coordinate=wgs84`
      break
    case 'bd': //百度地图
      Url = `https://api.map.baidu.com/direction?origin=latlng:${myLocation.lat},${myLocation.lng}|name:起点&destination=latlng:${destination.latitude},${destination.longitude}|终点:${destination.projectName}&mode=driving&region=${myLocation.city}&coord_type=wgs84&output=html&src=webapp.baidu.openAPIdemo`
      break
  }
  window.open(Url, '_self')
  // location.href = Url;
}
const showDetail = projectDetail => {
  router.push({
    name: 'projectDetail',
    query: { ...projectDetail }
  })
}
</script>

<script lang="ts">
</script>

<style lang="less" scoped>
.swiper-wrap {
  //  border-radius: 10px;
  // position: absolute;
  // position: fixed;
  // bottom: 16px;
   width: 100%;
  position: absolute;
  bottom: 16px;
  
  .back-swipe {
    // margin-left:8px;
    // width:351px;
    
     margin-left:8px;
  }
}

.major-event-detail-content {
  width: 311px;
  height: 160px;
  margin-left:8px;
  border-radius: 10px;
  background: #ffffff;
  color: #626262;
  box-shadow: 0px 3px 5px 0px #00000014;
  position: relative;
   left:8px;
  // right: 16px;
  // bottom: 16px;
  padding: 10px 15px;
  overflow: auto;
  .title {
    color: #0e0d16;
    font-weight: bold;
    font-size: 18px;
    margin-top: 10px;
    line-height: 24px;
    text-overflow: hidden;
    word-break: break-all;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .info {
    border-radius: 6px;
    padding: 0px 0 0 0px;
    font-size: 12px;
    // line-height: 22px;
    .desc {
      color: #ff8326;
      font-size: 11px;
      font-family: 'PingFang SC';
      background-color: rgba(255, 131, 38, 0.1);
      display: inline-block;
      padding: 0 5px;
      border-radius: 5px;
    }
  }
  .info-bottom {
    padding: 10px 0 0px 0px;
    font-size: 12px;
    font-family: 'PingFang SC';
    color: #626262;
    .split-line {
      width: 1px;
      height: 11px;
      background-color: #eaeaea;
      margin: 0 10px 0 10px;
    }
  }
  .desc {
    margin-top: 7px;
    font-size: 14px;
    line-height: 22px;
  }
  .images {
    margin-top: 3px;
    display: flex;
    width: 100%;
    height: 32px;
    align-items: center;
    flex-direction: row-reverse;
    .attention {
      width: 80px;
      height: 32px;
      border-radius: 18px;
      font-size: 12px;
      line-height: 30px;
      border: 0.5px solid #ff8326;
      display: flex;
      font-family: 'PingFang SC';
      justify-content: center;
      align-items: center;
      .attention-img {
        background: url('@/assets/project/attentioned.png') no-repeat center;
        background-size: 15px 15px;
        padding-left: 40px;
      }
    }
    .navigation {
      width: 80px;
      height: 32px;
      border-radius: 18px;
      font-size: 12px;
      line-height: 30px;
      border: 0.5px solid #ff8326;
      display: flex;
      font-family: 'PingFang SC';
      background-color: #ff8326;
      color: #fff;
      margin: 0 0 0 8px;
      justify-content: center;
      align-items: center;
      .navigation-img {
        background: url('@/assets/project/navigation.png') no-repeat center;
        background-size: 15px 15px;
        padding-left: 40px;
      }
    }
    img {
      // border-radius: 5px;
      width: 15px;
      height: 15px;
      // margin: 0 7px 7px 0;
      &:nth-child(4n) {
        margin-right: 0;
      }
    }
    .btn-name {
      color: #ff8326;
      margin-left: 2px;
    }
  }
  
}
.navigationList {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    text-align: center;
    padding: 0 10px 6px;
    background-color: #fff;
    border-radius: 5% 5% 0 0;
    z-index: 1300;
    p {
      padding: 10px;
      color: #1937d8;
    }
    p:nth-child(2) {
      border-top: 1px solid #ccc;
      border-bottom: 1px solid #ccc;
    }
  }
.van-loading {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1010;
  background-color: #fff;
  padding: 20px;
  border-radius: 5%;
}
</style>

